package com.abocode.aichat.server.modules.protocol.handler;

import com.abocode.aichat.server.channel.ChannelWrapper;
import com.abocode.aichat.server.context.ServerRuntimeContext;
import com.abocode.aichat.server.entity.Entity;
import com.abocode.aichat.server.entity.EntityImpl;
import com.abocode.aichat.server.modules.ModuleHandler;
import com.abocode.aichat.server.reply.Replies;
import com.abocode.aichat.server.stanza.Stanza;
import com.abocode.aichat.server.stanza.StanzaWrapper;
import com.abocode.aichat.server.channel.ChannelWrapper;
import com.abocode.aichat.server.context.ServerRuntimeContext;
import com.abocode.aichat.server.entity.Entity;
import com.abocode.aichat.server.entity.EntityImpl;
import com.abocode.aichat.server.reply.Replies;
import com.abocode.aichat.server.stanza.Stanza;
import com.abocode.aichat.server.stanza.StanzaWrapper;

import java.util.Map;

/**
 * Created by franky.
 */
public class PresenceSetHandler implements ModuleHandler {
    @Override
    public Replies execute(ChannelWrapper channelWrapper, Stanza stanza, ServerRuntimeContext serverRuntimeContext) {
        Map body = stanza.body();
        String presence = String.valueOf(body.get("presence"));
        String jid = String.valueOf(body.get("jid"));
        Entity entity = EntityImpl.parse(jid);
        entity.setPresence(presence);
        serverRuntimeContext.getProtocolChannelRegistry().presence(entity);
        Replies replies = new Replies();
        replies.addReply(channelWrapper.getChannel(), StanzaWrapper.wrapperSuccess(stanza));
        return replies;
    }

    @Override
    public String module() {
        return "set";
    }
}
